/**
 * 自定义函数
 * */


//获取工时
function getOne(){
    var uinfo = getUserInfo();
    var formData = {
        id: id, depart_id: uinfo.depart_id, uid: uinfo.uid,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'whyw/one',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            console.log(res.data);
            oneHtml(res.data);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
function oneHtml(e){
    var d = e.data;
    var itemList = e.item;
    var taskList = e.task;
    var h, i;
    //日期
    $('#in_day')[0].value = d.in_day;
    //项目
    h = '';
    if (d.item_id == 0) {
        h += '<option value="0" selected>-选择项目-</option>';
    } else {
        h += '<option value="0">-选择项目-</option>';
    }
    for (i = 0; i < itemList.length; i ++) {
        if (d.item_id == itemList[i].id) {
            h += '<option value="' + itemList[i].id + '" selected>';
        } else  {
            h += '<option value="' + itemList[i].id + '">';
        }
        if (d.item_default == itemList[i].id) {
            h += '【默认项】';
        }
        h += itemList[i].title;
        h += '</option>';
    }
    $('#item_id').html(h);
    //任务
    h = '';
    if (d.task_id == 0) {
        h += '<option value="0" selected>-选择任务-</option>';
    } else {
        h += '<option value="0">-选择任务-</option>';
    }
    for (i = 0; i < taskList.length; i ++) {
        if (d.task_id == taskList[i].id) {
            h += '<option value="' + taskList[i].id + '" selected>';
        } else {
            h += '<option value="' + taskList[i].id + '">';
        }
        h += taskList[i].title;
        h += '</option>';
    }
    $('#task_id').html(h);
    $('#delay_total')[0].value = d.delay;
    ///////
    var dwh = d.whList, ii, child;
    h = '';
    for (i = 0; i < dwh.length; i ++) {
        child = dwh[i].child;
        if (child.length > 0) {
            h += '<div class="fgroup fchild">';
            h += '<div class="group">';
            h += '<div class="title">';
            h += '工时项：';
            h += '<span class="fedit" onclick="cntAdd();">新增内容</span>';
            //h += '<select id="task_id">';
            //h += '<option value="">-选择-</option>';
            //h += '</select>';
            h += '<input type="text" class="input-text" value="' + dwh[i].config_title + '" readonly/>';
            h += '</div>';
            h += '</div>';
            h += '<div class="group">';
            h += '<div class="title">';
            h += '合计工时：';
            h += '<input type="text" class="input-text" value="' + dwh[i].delay + '" readonly/>';
            h += '</div>';
            h += '</div>';
        }
        for (ii = 0; ii < child.length; ii ++) {
            h += '<div class="fgroup">';
            h += '<div class="group">';
            h += '<div class="title">';
            h += '工作内容：';
            h += '<span class="fdel" onclick="cntDel(' + child[ii].id + ');">删除</span>';
            h += '<span class="fedit" onclick="cntEdit(' + child[ii].id +  ');">再次编辑</span>';
            //h += '<select id="cnt_id">';
            //h += '<option value="">-选择-</option>';
            //h += '</select>';
            h += '<input type="text" class="input-text" value="' + child[ii].cnt_str + '" readonly/>';
            h += '</div>';
            h += '</div>';
            h += '<div class="group">';
            h += '<div class="title">';
            h += '开始时间：';
            //h += '<select id="starthour">';
            //h += '<option value="">-选择-</option>';
            //h += '</select>';
            h += '<input type="text" class="input-text" value="' + child[ii].starthour + '" readonly/>';
            h += '</div>';
            h += '</div>';
            h += '<div class="group">';
            h += '<div class="title">';
            h += '结束时间：';
            //h += '<select id="endhour">';
            //h += '<option value="">-选择-</option>';
            //h += '</select>';
            h += '<input type="text" class="input-text" value="' + child[ii].endhour + '" readonly/>';
            h += '</div>';
            h += '</div>';
            h += '<div class="group">';
            h += '<div class="title">';
            h += '时长：';
            h += '<input type="text" class="input-text" value="' + child[ii].delay_str + '" readonly/>';
            h += '</div>';
            h += '</div>';
            h += '</div>';
        }
        h += '</div>';
    }
    $('#tlist').html(h);
}


//设置项目
function setByItem(){
    var uinfo = getUserInfo();
    var item_id = $('#item_id').val();
    var task_id = $('#task_id').val();
    var in_day = $('#in_day').val();
    var formData = {
        id: id, item_id: item_id, task_id: task_id, uid: uinfo.uid,
        role_id: uinfo.role_id, depart_id: uinfo.depart_id,
        in_day: in_day,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'whyw/setbyitem',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            //window.location.reload();
            window.location.href = 'whyw-edit.html?id=' + res.data;
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//新增工时项
function cntAdd(){
    var item_id = $('#item_id').val();
    if (item_id == 0 || item_id == '') {
        msgPop({msg: '项目必须的'}); return
    }
    window.location.href = 'whyw-cnt.html?id=0&wh_id=' + id + '&item_id=' + item_id;
}


//新增工时项
function cntEdit(v){
    var item_id = $('#item_id').val();
    if (item_id == 0 || item_id == '') {
        msgPop({msg: '项目必须的'}); return
    }
    window.location.href = 'whyw-cnt.html?id=' + v + '&wh_id=' + id + '&item_id=' + item_id;
}


//删除
function cntDel(v){
    var formData = {
        id: v,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'whitem/del',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            window.location.reload();
            //window.location.href = 'whyw-edit.html?id=' + res.data;
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//保存
function save(){
    var formData = {
        id: id,
    };
    //console.log(formData);
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'whyw/save',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                //window.top.location.reload();
                window.location.href = 'whyw.html';
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//发布
function pub(){
    var formData = {
        id: id,
    };
    //console.log(formData);
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'whyw/pub',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                //window.top.location.reload();
                window.location.href = 'whyw.html';
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}